<?

#	This file is part of SmartWebIF.
#	
#	SmartWebIF is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	SmartWebIF is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with SmartWebIF.  If not, see <http://www.gnu.org/licenses/>.

$config = include 'config.php';
include 'functions.php';

if (($config["password"] <> '') and ($_COOKIE["auth"] <> $config["password"]) or ($_COOKIE["time"] <> '' and $_COOKIE["time"] < time())) die();

$sdvb = get_sdvb_info($config);
if ($sdvb["data_path_err"]) die();

echo '
<!doctype html>
<html lang="en">
<head>
<script>
  $(function() {$(".b-refresh").button({icons:{primary:"ui-icon-refresh"}});});
  $(function() {$(".b-play").button({icons:{primary:"ui-icon-play"}});});
  $(function() {$(".b-delete").button({icons:{primary:"ui-icon-trash"}});});
  $(function() {
    $( "#accordion-dvr1" ).accordion({
	  collapsible: true,
	  heightStyle: "content"
	  });
	});
  $(function() {
    $( "#accordion-dvr2" ).accordion({
	  collapsible: true,
	  heightStyle: "content"
	  });
	});
</script>
</head>
';

echo '<body>';
echo '<table width="100%"><tr><td align="right" valign="top"><form><button type="submit" name="tab" value="2" class="b-refresh">Refresh DVR</button></form></td></tr></table>';

echo '<table><tr><td id="left_column">';
$dir = scandir($_SERVER[SYSTEMROOT].'\\tasks\\');
$hasrec = 0;
foreach ($dir as $file) if (substr($file,0,10) == 'SmartWebIF') $hasrec = 1;
if ($hasrec == 1)
	{	
	echo '<fieldset>';
	echo '<legend> Scheduled recordings </legend>';
	echo '<div id="accordion-dvr1">';
	$tasks = shell_exec('schtasks /Query /FO CSV');
	$csvtasks = str_getcsv($tasks, "\n");
	foreach($csvtasks as $csvtask)
		{
		$csvtask = str_getcsv($csvtask);
		if (substr($csvtask[0],1,10) == 'SmartWebIF')
			{
			if ($csvtask[1] == 'N/A') delete_task(substr($csvtask[0],1,24));
			echo '<h3><b>'.substr($csvtask[0],26).'</b></h3>';
			echo '<div>';
			echo 'Scheduled: '.$csvtask[1].'<br>';
			echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status: '.$csvtask[2].'<br>';
			echo '<br><form method="get" action="index.php"><input type="hidden" name="control" value="delete_task"><input type="hidden" name="task" value="'.substr($csvtask[0],1,24).'"><button type="submit" class="b-delete">Delete task</button></form>';
			echo '</div>';
			}
		}
	echo '</div>';
	echo '</fieldset>';
	}
else echo 'No scheduled recordings.';

echo '</td>';

echo '<td id="right_column">';

switch ($config["date_format"])
	{
	case 1:
	$dateformat = 'd/m/Y';
	break;
	case 2:
	$dateformat = 'm/d/Y';
	break;
	case 3:
	$dateformat = 'Y/m/d';
	break;
	} 
$dir = scandir($config["rec_path"]);
$key = array_search('.', $dir);
unset ($dir[$key]);
$key = array_search('..', $dir);
unset ($dir[$key]);
if (file_exists($config["rec_path"]) and (count($dir) > 0))
	{
	echo '<fieldset>';
	echo '<legend> Recorded channels </legend>';
	echo '<div id="accordion-dvr2">';
	foreach ($dir as $file)
		{
		$filename = mb_convert_encoding($file, "UTF-8", "ANSI, ISO-8859-2, ISO-8859-1");
		$fullfile = $config["rec_path"].$file;
		echo '<h3><b>'.$filename.'</b></h3>';
		echo '<div>';
		echo '<table width="100%" cellpadding="5"><tr>';
		echo '<td align="right" width="80">';
		echo 'Recorded on: <br>';
		echo 'Size: ';
		echo '</td>';
		echo '<td>';
		echo date($dateformat,filectime($fullfile)).'<br>';
		echo number_format(filesize($fullfile),0,',','.').' bytes<br>';
		echo '</td>';
		echo '</tr><tr>';
		echo '<td align="right"><form method="get" action="index.php"><input type="hidden" name="control" value="play"><input type="hidden" name="video" value="'.$filename.'"><button type="submit" class="b-play">Play</button></form></td>';
		echo '<td><form method="get" action="index.php"><input type="hidden" name="control" value="delete_recording"><input type="hidden" name="file" value="'.base64_encode($file).'"><button type="submit" class="b-delete">Delete</button></form></td>';
		echo '</tr></table>';
		echo '</div>';
		}
	
	echo '</div>';
	echo '</fieldset>';
	echo '</td></tr></table>';
	}
else echo 'No recorded channels.';
echo '</body>';
echo '</html>';

?>